package com.health.viewscontrols.medical;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.TimeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.health.R;
import com.health.db.FDbManager;
import com.health.tablebean.ARCHIVE_BASEINFO;
import com.health.tablebean.CD_DIABETESFOLLOWUP;
import com.health.tablebean.CD_HYPERTENSIONFOLLOWUP;
import com.health.viewscontrols.NovaApplication;
import com.health.viewscontrols.medical.adapter.MedicalAdapter;
import com.health.viewscontrols.medical.adapter.SfHistoryAdapter;
import com.health.viewscontrols.medical.base.BasePtActivity;
import com.health.viewscontrols.medical.base.DeviceEnum;
import com.health.viewscontrols.medical.bean.SfHistoryBean;
import com.nova.health.ble.BleConfigConstants;
import com.orhanobut.hawk.Hawk;

import java.util.ArrayList;
import java.util.List;

public class SfHistoryActivity extends BasePtActivity {

    protected LinearLayout ll_finish;
    protected TextView tv_title;
    private RecyclerView recyclerView;
    private SfHistoryAdapter sfHistoryAdapter;
    private List<SfHistoryBean> historyBeanList = new ArrayList<>();
    private FDbManager fdbmanger = null;
    public ARCHIVE_BASEINFO userInfo;
    private String type;

    String strWhere = "IDCARD = '" + NovaApplication.getUserInfo().getIDCARD() + "'";

    @Override
    public int bindLayout() {
        return R.layout.activity_sf_history;
    }

    @Override
    public void initData(@Nullable Bundle bundle) {

    }

    @Override
    public void initView(Bundle savedInstanceState, View contentView) {
        type = getIntent().getStringExtra("type");
//        ll_finish = contentView.findViewById(com.nova.health.ble.R.id.ll_finish);
        tv_title = contentView.findViewById(R.id.tv_left_title);
        recyclerView = contentView.findViewById(R.id.recyclerView);
        if ("gxy".equals(type)) {
            tv_title.setText("高血压随访记录");
        } else if ("tnb".equals(type)) {
            tv_title.setText("糖尿病随访记录");
        }

//        ll_finish.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                finish();
//            }
//        });
    }

    private void initData() {
        historyBeanList.clear();
        if ("gxy".equals(type)) {
            List<CD_HYPERTENSIONFOLLOWUP> list = fdbmanger.findAllByWhere(CD_HYPERTENSIONFOLLOWUP.class,
                    strWhere,"ID DESC");
            for (int i = 0; i < list.size(); i++) {
                CD_HYPERTENSIONFOLLOWUP cdHypertensionfollowup = list.get(i);
                SfHistoryBean historyBean = new SfHistoryBean();
                historyBean.setId(cdHypertensionfollowup.getID());
                historyBean.setName(userInfo.getCUSTOMERNAME());
                historyBean.setIdCard(userInfo.getIDCARD());
                historyBean.setType(type);
                historyBean.setFOLLOWUPDATE(cdHypertensionfollowup.getFOLLOWUPDATE());
                historyBean.setFollowupDate(TimeUtils.date2String(cdHypertensionfollowup.getFOLLOWUPDATE(), "yyyy-MM-dd"));
                historyBeanList.add(historyBean);
            }
        } else if ("tnb".equals(type)) {
            List<CD_DIABETESFOLLOWUP> list = fdbmanger.findAllByWhere(CD_DIABETESFOLLOWUP.class,
                    strWhere, "ID DESC");
            for (int i = 0; i < list.size(); i++) {
                CD_DIABETESFOLLOWUP cdHypertensionfollowup = list.get(i);
                SfHistoryBean historyBean = new SfHistoryBean();
                historyBean.setId(cdHypertensionfollowup.getID());
                historyBean.setName(userInfo.getCUSTOMERNAME());
                historyBean.setIdCard(userInfo.getIDCARD());
                historyBean.setType(type);
                historyBean.setFOLLOWUPDATE(cdHypertensionfollowup.getFOLLOWUP_DATE());
                historyBean.setFollowupDate(TimeUtils.date2String(cdHypertensionfollowup.getFOLLOWUP_DATE(), "yyyy-MM-dd"));
                historyBeanList.add(historyBean);
            }
        }
    }

    @Override
    public void doBusiness() {
        fdbmanger = NovaApplication.fdbmanger;
        userInfo = NovaApplication.getUserInfo();

        initData();

        initAdapter();
        sfHistoryAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                SfHistoryBean historyBean = (SfHistoryBean) adapter.getData().get(position);
                Intent intent = new Intent();
                intent.putExtra("value", historyBean);
                setResult(Activity.RESULT_OK,intent);
                finish();
            }
        });

        sfHistoryAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                try {
                    if (view.getId() == R.id.tv4) {
                        SfHistoryBean historyBean = (SfHistoryBean) adapter.getData().get(position);


                        dialog(historyBean);

                    } else if (view.getId() == R.id.tv3) {
                        SfHistoryBean historyBean = (SfHistoryBean) adapter.getData().get(position);
                        Intent intent = new Intent();
                        intent.putExtra("value", historyBean);
                        setResult(Activity.RESULT_OK,intent);
                        finish();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    private void initAdapter() {
        sfHistoryAdapter = new SfHistoryAdapter(R.layout.item_sf_history_list, historyBeanList,true);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(sfHistoryAdapter);

        DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL);
        recyclerView.addItemDecoration(dividerItemDecoration);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }

    private void dialog(SfHistoryBean historyBean) {
        new AlertDialog.Builder(this)
                .setTitle("提示")
                .setMessage("确定要删除（" + historyBean.getFollowupDate() + "）这条随访记录吗？")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String type = historyBean.getType();
                        if ("gxy".equals(type)) {
                            CD_HYPERTENSIONFOLLOWUP cdHypertensionfollowup = (CD_HYPERTENSIONFOLLOWUP) fdbmanger.findById(historyBean.getId(), CD_HYPERTENSIONFOLLOWUP.class);
                            fdbmanger.delete(cdHypertensionfollowup);
                        } else if ("tnb".equals(type)) {
                            fdbmanger.findById(historyBean.getId(), CD_DIABETESFOLLOWUP.class);
                            CD_DIABETESFOLLOWUP cdDiabetesfollowup = (CD_DIABETESFOLLOWUP) fdbmanger.findById(historyBean.getId(), CD_DIABETESFOLLOWUP.class);
                            fdbmanger.delete(cdDiabetesfollowup);
                        }
                        initData();
                        ToastUtils.showShort("删除成功！");
                        sfHistoryAdapter.setNewData(historyBeanList);
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // 用户点击了“取消”按钮，执行相应操作
                    }
                })
                .show();
    }
}
